import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:freemusic/model/migu/MiguSong.dart';

class MiGuAPI {
  static Future<List<MiguSong>> search(String keyword, int page) async {
    Response response = await Dio().get(
        "http://m.music.migu.cn/migu/remoting/scr_search_tag?keyword=$keyword&pgc=$page&type=2&rows=10");

    return MiguSong().fromRemoteJosn(response.data);
  }

  static Future<MiguSong> getSong(MiguSong song) async {
    Dio dio = Dio();
    dio.options.headers = {
      "Referer": "http://music.migu.cn/v3/music/player/audio",
    };
    Response lyricResponse = await dio.get(
      "http://music.migu.cn/v3/api/music/audioPlayer/getLyric?copyrightId=${song.copyrightId}",
    );
    final lyric = lyricResponse.data;
    song.lyrics = lyric['lyric'];

    String id = song.hash.split("_")[1];

    Response picResponse = await dio.get("http://music.migu.cn/v3/api/music/audioPlayer/getSongPic?songId=$id");
    final picMap = picResponse.data;
    song.img = "https:${picMap['mediumPic']}";
    return song;
  }

  static download(String url, String savepath) async {
    Response response = await Dio().download(url, savepath);
    return response;
  }
}
